<template lang="pug">
.login(:style="{ 'background-image': `url(${loginBg})` }")
  .login-form(:style="{ 'background-image': `url(${loginFormBg})` }")
    .login-form-left
      img.logo(:src="logo")
      .logo-title 有料系统管理平台
    .login-form-right
      .login-form-title 登录
      .login-form-body
        .form-item
          .form-item-label 用户名/手机号
          .form-item-bd
            el-input(v-model="form.userName" autocomplete="new-password" placeholder="请输入用户名/手机号" @keyup.enter.native="login")
        .form-item
          .form-item-label 密码
          .form-item-bd
            el-input(v-model="form.employeePassword", :type="pswType" autocomplete="new-password" placeholder="请输入密码" @keyup.enter.native="login")
              i.el-input__icon.form-item-icon(slot="suffix" @click="changePswType", :class="pswType === 'password' ? 'el-icon-view' : 'icon-close-eyes'")
        .form-item
          el-checkbox(v-model="isRemember" true-label="1" false-label="0") 记住密码
        .form-item-handle(:class="{ 'disabled': !disabled }" @click="login")
          span 登录
          i.iconfont.el-icon-right
</template>

<script>
import { postLogin, getUserInfo } from 'api'
import { hex_md5_salt } from '@/utils/md5'
import { setCookie } from 'utils/cookie'
import { base64decode, base64encode } from 'utils/base64'
import { mapActions } from 'vuex'
export default {
  name: 'login',
  data () {
    return {
      loginBg: require('../../static/images/login_bj.png'),
      loginFormBg: require('../../static/images/mask_group.png'),
      logo: require('../../static/images/login_logo.png'),
      form: {
        userName: '',
        employeePassword: ''
      },
      isRemember: 0,
      pswType: 'password'
    }
  },
  computed: {
    disabled () {
      return !!this.form.userName && !!this.form.employeePassword
    }
  },
  methods: {
    ...mapActions([
      'changeUserInfo'
    ]),
    changePswType () {
      this.pswType = this.pswType === 'password' ? 'text' : 'password'
    },
    async login () {
      try {
        if (!this.disabled)return
        const { ResultSet } = await this.$axios.post(postLogin, { ...this.form, employeePassword: hex_md5_salt(encodeURIComponent(this.form.employeePassword)) })
        if (this.isRemember) this.setStorage(this.form.userName, base64encode(this.form.employeePassword))
        else this.clearStorage()
        this.getUserinfo()
        setCookie('JSESSIONID', ResultSet.userData.token, '/')
        this.$router.replace({ name: 'index' })
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getUserinfo () {
      try {
        const { ResultSet } = await this.$axios.post(getUserInfo)
        this.changeUserInfo(ResultSet.userData)
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    setStorage (c_name, c_pwd) {
      localStorage.setItem('isRemember', this.isRemember)
      localStorage.setItem('userName', c_name)
      localStorage.setItem('userPwd', c_pwd)
    },
    //清除cookie
    clearStorage () {
      localStorage.removeItem('isRemember')
      localStorage.removeItem('userName')
      localStorage.removeItem('userPwd')
    },
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      const isRemember = localStorage.getItem('isRemember')
      vm.isRemember = isRemember ? isRemember : '0'
      if (vm.isRemember) {
        const userName = localStorage.getItem('userName')
        const userPwd = localStorage.getItem('userPwd')
        vm.form.userName = userName
        if (userPwd) {
          vm.form.employeePassword = base64decode(userPwd)
        } else {
          vm.form.employeePassword = ''
        }
      }
    })
  }
} 
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  width: 100%;
  height: 100vh;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
.login-form {
  width: 1104px;
  height: 688px;
  background: #FFFFFF;
  box-shadow: 0px 20px 60px rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 40px;
  transform: translate(-50%, -50%);
  background-repeat: no-repeat;
  background-size: contain;
  display: flex;
}
.login-form-left,
.login-form-right {
  position: relative;
  width: 50%;
}
.logo-title {
  position: absolute;
  top: 37%;
  left: 0;
  font-size: 28px;
  line-height: 37px;
  font-weight: bold;
  color: #FFFFFF;
  letter-spacing: 0.6em;
}
.login-form-right {
  padding-left: 70px;
  padding-top: 85px;
}
.login-form-title {
  font-weight: bold;
  font-size: 28px;
  line-height: 37px;
  color: #3E4373;
}
.login-form-body {
  margin-top: 60px;
}
.form-item {
  margin-bottom: 24px;
}
.form-item-label {
  font-size: 14px;
  line-height: 24px;
  color: #3E4373;
}
.form-item-bd {
  margin-top: 4px;
  width: 344px;
}
.form-item-icon {
  font-size: 16px;
}
.form-item-handle {
  width: 344px;
  height: 40px;
  background: #FF2E42;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  position: relative;
  cursor: pointer;
  border-radius: 6px;
  span {
    display: inline-block;
    font-weight: bold;
    font-size: 16px;
    line-height: 21px;
  }
  .iconfont {
    position: absolute;
    top: 50%;
    right: 18px;
    font-size: 20px;
    transform: translateY(-50%);
  }
  &.disabled {
    background: #F6F6F9;
    color: #8B8EAB;
    cursor: no-drop;
  }
}
</style>
